package com.hk.core.autoconfigure.condition;

import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

/**
 * @author Kevin
 */
@Deprecated
public class ClientTokenCondition extends SpringBootCondition {

    public static boolean hasClientToken(Environment environment) {
        var keyValue = environment.getProperty("security.oauth2.resource.jwt.key-value");
        var keyUri = environment.getProperty("security.oauth2.resource.jwt.key-uri");
        return StringUtils.hasText(keyValue) || StringUtils.hasText(keyUri);
    }

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context,
                                            AnnotatedTypeMetadata metadata) {
        var message = ConditionMessage.forCondition("OAuth JWT Condition");
        var environment = context.getEnvironment();
        if (hasClientToken(environment)) {
            return ConditionOutcome
                    .match(message.foundExactly("provided public key"));
        }
        return ConditionOutcome
                .noMatch(message.didNotFind("provided public key").atAll());
    }
}
